# coding=utf-8
import os
import sys

from catch_bag import CatchBag
from catch_config import CatchConfig
from catch_logging import CatchLogging
from json_db import JsonDB
from post_data_thread import PostDataThread

BASE_URL = os.path.dirname(os.path.realpath(sys.argv[0]))
if __name__ == '__main__':

    try:
        # 设置系统编码
        reload(sys)
        sys.setdefaultencoding('utf8')

        CatchLogging.base_url = BASE_URL
        JsonDB.base_url = BASE_URL

        # 读取配置文件
        catch_config = CatchConfig(BASE_URL).get_catch_config()
        iface_str = catch_config['iface_str']
        filter_str = catch_config['filter_str']
        post_url = catch_config['post_url']

        # 开始抓包
        catch_bag = CatchBag(iface_str, filter_str)
        catch_bag.sniff(lambda data: PostDataThread.post_data(post_url, data))
    except Exception as e:
        CatchLogging.error(e.message)




